﻿<template>
    <div class="l-rblock l-form-viewer" style="padding:8px;">
        <el-form :model="formData" :rules="rules" size="mini" labelPosition="right" labelWidth="110px" ref="form">
            <div class="l-auto-window only-tabs">
                <el-tabs v-model="activeName" type="card">
                    <el-tab-pane class="l-tabs-container" :label="$t('主表信息')" name="tab0">
                        <el-row :gutter="16">
                            <div class="l-rblock">
                                <el-col v-if="lr_formLookAuth('case_erp_customergather_f_Name')" :span="24">
                                    <el-form-item :label="$t('客户名称')" prop="case_erp_customergather_f_Name">
                                        <l-select
                                            :disabled="!lr_formEditAuth('case_erp_customergather_f_Name', false || isReadOnly)"
                                            :options="case_erp_customergather_f_NameOptions"
                                            v-model="formData.case_erp_customergather_f_Name" />
                                    </el-form-item>
                                </el-col>
                                <el-col v-if="lr_formLookAuth('case_erp_customergather_f_SaleId')" :span="24">
                                    <el-form-item :label="$t('销售订单')" prop="case_erp_customergather_f_SaleId">
                                        <l-select
                                            :disabled="!lr_formEditAuth('case_erp_customergather_f_SaleId', false || isReadOnly)"
                                            :options="case_erp_customergather_f_SaleIdOptions"
                                            v-model="formData.case_erp_customergather_f_SaleId" @change="changeSale" />
                                    </el-form-item>
                                </el-col>

                                <el-col v-if="lr_formLookAuth('case_erp_customergather_f_WaitAmount')" :span="24">
                                    <el-form-item :label="$t('计划回款金额')" prop="case_erp_customergather_f_WaitAmount">
                                        <el-input disabled v-model="formData.case_erp_customergather_f_WaitAmount"
                                            :placeholder="$t('please enter')">
                                        </el-input>
                                    </el-form-item>
                                </el-col>
                                <el-col v-if="lr_formLookAuth('case_erp_customergather_f_ReceivedDate')" :span="24">
                                    <el-form-item :label="$t('计划回款日期')" prop="case_erp_customergather_f_ReceivedDate">
                                        <l-date
                                            :disabled="!lr_formEditAuth('case_erp_customergather_f_ReceivedDate', isReadOnly)"
                                            :readonly="false" format="yyyy-MM-dd" dateType="date" :clearable="true"
                                            v-model="formData.case_erp_customergather_f_ReceivedDate"
                                            :placeholder="$t('please select')" />
                                    </el-form-item>
                                </el-col>
                                <el-col v-if="lr_formLookAuth('case_erp_customergather_f_FinallyDate')" :span="24">
                                    <el-form-item :label="$t('最迟回款日期')" prop="case_erp_customergather_f_FinallyDate">
                                        <l-date
                                            :disabled="!lr_formEditAuth('case_erp_customergather_f_FinallyDate', isReadOnly)"
                                            :readonly="false" format="yyyy-MM-dd" dateType="date" :clearable="true"
                                            v-model="formData.case_erp_customergather_f_FinallyDate"
                                            :placeholder="$t('please select')" />
                                    </el-form-item>
                                </el-col>
                                <el-col v-if="lr_formLookAuth('case_erp_customergather_f_Principal')" :span="24">
                                    <el-form-item :label="$t('合同负责人')" prop="case_erp_customergather_f_Principal">
                                        <l-user-select
                                            :disabled="!lr_formEditAuth('case_erp_customergather_f_Principal', isReadOnly)"
                                            v-model="formData.case_erp_customergather_f_Principal" />
                                    </el-form-item>
                                </el-col>
                                <el-col v-if="lr_formLookAuth('case_erp_customergather_f_Title')" :span="24">
                                    <el-form-item :label="$t('合同标题')" prop="case_erp_customergather_f_Title">
                                        <el-input
                                            :disabled="!lr_formEditAuth('case_erp_customergather_f_Title', isReadOnly)"
                                            v-model="formData.case_erp_customergather_f_Title" :placeholder="$t('please enter')">
                                        </el-input>
                                    </el-form-item>
                                </el-col>
                                <el-col v-if="lr_formLookAuth('case_erp_customergather_f_File')" :span="24">
                                    <el-form-item :label="$t('合同附件')" prop="case_erp_customergather_f_File">
                                        <l-upload drag maxSize="10" sizeType="MB" :limit="1" showFileList="uploadTheme1"
                                            :disabled="!lr_formEditAuth('case_erp_customergather_f_File', false || isReadOnly)"
                                            v-model="formData.case_erp_customergather_f_File"
                                            :placeholder="$t('please enter')" />
                                    </el-form-item>
                                </el-col>
                            </div>
                        </el-row>
                    </el-tab-pane>

                </el-tabs>
            </div>
        </el-form>
    </div>
</template>
<script>
const api = window.$api.erpcase.customerGather

export default {
    props: {
        isReadOnly: {
            type: Boolean,
            default: false
        }
    },
    data() {
        return {
            moduleCode: 'ERP_CustomerReturn',
            activeName: 'tab0',
            formData: {
                case_erp_customergather_f_SaleId: '', // 订单信息
                case_erp_customergather_f_Name: "", // 客户名称
                case_erp_customergather_f_WaitAmount: "", // 计划回款金额
                case_erp_customergather_f_ReceivedDate: "", // 计划回款日期
                case_erp_customergather_f_FinallyDate: "", // 最迟回款日期
                case_erp_customergather_f_Principal: "", // 合同负责人
                case_erp_customergather_f_Title: "", // 合同标题
                case_erp_customergather_f_File: "", // 合同附件

            },
            rules: {
                case_erp_customergather_f_Name: [
                    { required: true, message: 'please select客户名称' },
                ],
                case_erp_customergather_f_SaleId: [
                    { required: true, message: 'please select销售订单' },
                ],
                case_erp_customergather_f_WaitAmount: [
                    { required: true, message: 'please enter计划回款金额' },
                    // { pattern: /^-?[0-9]+$/, message: 'please enter数字' },
                ],
                case_erp_customergather_f_FinallyDate: [
                    { required: true, message: 'please select计划回款日期' },
                ],
                case_erp_customergather_f_ReceivedDate: [
                    { required: true, message: 'please select计划回款日期' },
                ],

            },

        };
    },
    computed: {
        case_erp_customergather_f_NameOptions() {
            const data = this.lr_DataSource('ERP_Customers')
            return this.lr_DataSourceOptions(data, 'f_name', 'f_name')
        },
        case_erp_customergather_f_SaleIdOptions() {
            const data = this.lr_DataSource('ERP_SaleList')
            return this.lr_DataSourceOptions(data, 'f_id', 'f_theme')
        },
    },
    methods: {
        // 选择订单
        changeSale(e) {
            // console.log("选择订单",e)
            this.formData.case_erp_customergather_f_WaitAmount = e.f_amountsum
        },


        // Reset表单
        resetForm() {
            this.$formClear(this.$refs.form)
            this.activeName = 'tab0'

        },
        // 校验表单
        async validateForm() {
            if (!await this.$formValidateWraper(this.$refs.form)) {
                return false
            }

            return true
        },
        // 设置表单数据
        setForm(data) {
            // console.log('设置1', data)
            this.$setFormData(this.formData, data, 'case_erp_customergather')
            // console.log('设置',this.formData)

        },
        // 获取表单数据
        getForm() {
            const formData = {}
            formData.caseErpCustomergatherEntity = this.$getFormData(this.formData, 'case_erp_customergather')
            return formData

        },

        async loadFormData({ keyValue/*,params,node*/ }) {//  keyValue 流程中相当于流程processId,params 传递参数,node 流程节点
            const data = await this.$awaitWraper(api.get(keyValue))
            // console.log("进入")
            if (data) {
                this.setForm(data)
                return true
            }
            else {
                return false
            }
        },

        async saveForm({ keyValue, isEdit/*,code,node*/ }) {// isEdit 是否更新数据, keyValue 流程中相当于流程processId,code 表示流程中的操作码,node 流程节点
            const postData = this.getForm()
            if (isEdit) {// Edit
                return await this.$awaitWraper(api.update(keyValue, postData.caseErpCustomergatherEntity))
            }
            else {// Add
                postData.caseErpCustomergatherEntity.f_Id = keyValue
                postData.caseErpCustomergatherEntity.f_AlreadyAmount = 0
                postData.caseErpCustomergatherEntity.f_UnpaidAmount = postData.caseErpCustomergatherEntity.f_WaitAmount

                return await this.$awaitWraper(api.add(postData.caseErpCustomergatherEntity))
            }
        },
    }
}
</script>